//
//  HTLeftMenuShowGuideView.m
//  HT_aigongzuo
//
//  Created by 周昭 on 16/10/11.
//  Copyright © 2016年 HT_Technology. All rights reserved.
//

#import "HTLeftMenuShowGuideView.h"
#import "HTLeftMenuDrawView.h"
#define KScreenWidth    [UIScreen mainScreen].bounds.size.width
#define KScreenHeight    [UIScreen mainScreen].bounds.size.height

@interface HTLeftMenuShowGuideView()
/**
 *  背景
 */
@property (nonatomic, weak) UIView *bgView;
/**
 *  凸显选择照相
 */
@property (nonatomic, weak) UIView *photoView;
/**
 *  详情文字
 */
@property (nonatomic, weak) UILabel *detailLabel;
/**
 *  确认按钮
 */
@property (nonatomic, weak) UIButton *sureBtn;
/**
 *  贝塞尔曲线
 */
@property (nonatomic, weak) HTLeftMenuDrawView *drawView;

@end

@implementation HTLeftMenuShowGuideView

 - (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
    
        [self setUpSubViews];
    }
    
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)decoder
{
    if (self = [super initWithCoder:decoder]) {
        
        [self setUpSubViews];
    }
    
    return self;
}

/**
 *  初始化所有子控件并且进行一次性设置
 */
- (void)setUpSubViews
{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.7;
    [self addSubview:bgView];
    self.bgView = bgView;
    
    UIView *photoView = [[UIView alloc] init];
    photoView.backgroundColor = [UIColor whiteColor];
    photoView.layer.cornerRadius = 5;
    [bgView addSubview:photoView];
    self.photoView = photoView;
    
    UILabel *detailLabel = [[UILabel alloc] init];
    detailLabel.text = NSLocalizedString(@"点击这里,\n可以选择照片或者拍照哟!", nil);
    detailLabel.textAlignment = NSTextAlignmentCenter;
    detailLabel.numberOfLines = 0;
    detailLabel.textColor = [UIColor whiteColor];
    detailLabel.font = [UIFont fontWithName:@"MarkerFelt-Wide" size:16.0f];
    [bgView addSubview:detailLabel];
    self.detailLabel = detailLabel;
    
    UIButton *sureBtn = [[UIButton alloc] init];
    sureBtn.layer.masksToBounds = YES;
    sureBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    sureBtn.layer.borderWidth = 1.5;
    sureBtn.layer.cornerRadius = 5;
    [sureBtn setTitle:NSLocalizedString(@"朕知道了", nil) forState:UIControlStateNormal];
    sureBtn.titleLabel.textColor = [UIColor whiteColor];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    [sureBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:sureBtn];
    self.sureBtn = sureBtn;
    
    HTLeftMenuDrawView *drawView = [[HTLeftMenuDrawView alloc] init];
    drawView.backgroundColor = [UIColor clearColor];
    [self addSubview:drawView];
    self.drawView = drawView;
}

/**
 *  拿到真实尺寸设置控件的frame
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat bgViewX = 0;
    CGFloat bgViewY = 0;
    CGFloat bgViewW = self.frame.size.width;
    CGFloat bgViewH = self.frame.size.height;
    self.bgView.frame = CGRectMake(bgViewX, bgViewY, bgViewW, bgViewH);
    
    CGFloat photoViewW = KScreenHeight == 568 ? 60 : 75;
    CGFloat photoViewX = 20 + photoViewW * 0.5;
    CGFloat photoViewY = 30 + photoViewW * 0.5;
    CGFloat photoViewH = photoViewW;
    self.photoView.center = CGPointMake(photoViewX, photoViewY);
    self.photoView.bounds = CGRectMake(0, 0, photoViewW, photoViewH);
    
    CGFloat detailLabelX = bgViewX;
    CGFloat detailLabelY = bgViewH * 0.3;
    CGFloat detailLabelW = bgViewW;
    CGFloat detailLabelH = 70;
    self.detailLabel.frame = CGRectMake(detailLabelX, detailLabelY, detailLabelW, detailLabelH);
    
    CGFloat sureBtnX = bgViewW * 0.5;
    CGFloat sureBtnH = 40;
    CGFloat sureBtnY = detailLabelY + detailLabelH + sureBtnH + 30;
    CGFloat sureBtnW = 100;
    self.sureBtn.center = CGPointMake(sureBtnX, sureBtnY);
    self.sureBtn.bounds = CGRectMake(0, 0, sureBtnW, sureBtnH);
    
    self.drawView.frame = CGRectMake(0, 0, KScreenWidth, detailLabelY);
    
    [self.drawView drawBezierPathWithControlP:CGPointMake(sureBtnX - 45, detailLabelY - 70) currentP:CGPointMake(sureBtnX - 15, detailLabelY - 20) endP:CGPointMake(photoViewX + photoViewW * 0.5 + 5, photoViewY + photoViewW * 0.5 + 3) rightP:CGPointMake(photoViewX + photoViewW * 0.5 + 14, (KScreenHeight == 568 ? photoViewY - 2 : photoViewY) + photoViewW * 0.5 + 3.3) leftP:CGPointMake(photoViewX + photoViewW * 0.5 + 6, photoViewY + photoViewW * 0.5 + 10) lineWidth:2.0 lineColor:[UIColor whiteColor]];
}

/**
 *  展示
 */
- (void)show
{
    UIWindow *appWindow = [[[UIApplication sharedApplication] delegate] window];
    [appWindow addSubview:self];
}

/**
 *  移除(通过动画)
 */
- (void)hide
{
    [UIView animateWithDuration:0.15 animations:^{
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
